package com.ruoyi.web.controller.project.patent;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.project.patent.Patent;
import com.ruoyi.system.domain.project.patent.PatentAuthorization;
import com.ruoyi.system.service.project.IPatentAuthorizationService;
import com.ruoyi.system.service.project.IPatentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 存储教师专利信息的Controller
 * 
 * @author ruoyi
 * @date 2025-07-30
 */
@RestController
@RequestMapping("/project/patent")
public class PatentController extends BaseController
{
    @Autowired
    private IPatentService patentService;
    @Autowired
    private IPatentAuthorizationService patentAuthorizationService;
    /**
     * 查询存储教师专利信息的列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Patent patent)
    {
        startPage();
        List<Patent> list = patentService.selectPatentList(patent);
        return getDataTable(list);
    }

    /**
     * 导出存储教师专利信息的列表
     */
    @Log(title = "存储教师专利信息的", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Patent patent)
    {
        List<Patent> list = patentService.selectPatentList(patent);
        ExcelUtil<Patent> util = new ExcelUtil<Patent>(Patent.class);
        util.exportExcel(response, list, "存储教师专利信息的数据");
    }

    /**
     * 获取存储教师专利信息的详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(patentService.selectPatentById(id));
    }

    /**
     * 新增存储教师专利信息的
     */
    @Log(title = "存储教师专利信息的", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Patent patent)
    {
        return toAjax(patentService.insertPatent(patent));
    }

    /**
     * 修改存储教师专利信息的
     */
    @Log(title = "存储教师专利信息的", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Patent patent)
    {
        return toAjax(patentService.updatePatent(patent));
    }

    /**
     * 删除存储教师专利信息的
     */
    @Log(title = "存储教师专利信息的", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        PatentAuthorization ra = new PatentAuthorization();
        ra.setPatentMainId(ids[0]);
        List<PatentAuthorization> list = patentAuthorizationService.selectPatentAuthorizationList(ra);
        if(!list.isEmpty()){
            return warn("该专利下有授权信息，暂无法删除，请先删除授权信息。");
        }

        return toAjax(patentService.deletePatentByIds(ids));
    }

    /**
     * 获取当前用户专利列表
     */
    @GetMapping("/listMyPatents")
    public AjaxResult listMyPatents(Patent patent)
    {
        List<Patent> list = patentService.listMyPatents(patent);
        return success(list);
    }
}
